//把数组转成treelist
export const convert = (arr: any[]) => {
  arr = JSON.parse(JSON.stringify(arr))
  let data: any = []
  arr.forEach(item => {
    item.children = arr.filter(v => item.deptId === v.parentId)
    //  找父节点
    arr.filter(v => item.parentId === v.deptId).length == 0 ? data.push(item) : ''
  })

  return data
}
//组织机构数据和组织机构区域块数据做循环比较添加县级code下的区域块列表
export const compareAndUpdateArray = (array: any, array1: any) => {
  let arr = []
  let newnum = -1
  for (let i = 0; i < array.length; i++) {
    let nums = i
    for (let j = 0; j < array1.length; j++) {
      if (array[i].code === array1[j].l3dept) {
        array1[j].disabled = false
        array1[j].deptId = array[i].deptId
        array1[j].sortOrder = array[i].sortOrder
        array1[j].parentId = array[i].parentId
        array1[j].level = array[i].level
        array1[j].code = array[i].code
        array1[j].value = array1[j].id
        array1[j].label = array1[j].teamname
        arr.push(array1[j])
      } else {
        if (newnum != nums) {
          array[i].disabled = true
          array[i].label = array[i].name
          array[i].value = array[i].code
          arr.push(array[i])
        }
        newnum = i
      }
    }
  }
  let removedArr = arr.filter(x => x.level != 4)
  return removedArr
}
//JS获取两个数组对象中相同元素组成新的数组用来做预案下拉选中和备选下拉选中同步
export const compareSameAndNewArray = (multipleSelection: any[], multipleSelectionnow: any[]) => {
  // 双重for循环，时间复杂度：n*n
  let result = []
  for (let i = 0; i < multipleSelection.length; i++) {
    let tempArr1 = multipleSelection[i]
    for (let j = 0; j < multipleSelectionnow.length; j++) {
      let tempArr2 = multipleSelectionnow[j]
      if (tempArr2.firefightingteamid === tempArr1.firefightingteamid) {
        result.push(tempArr2)
        break
      }
    }
  }
  return result
}

//根据通过数组AB做对比取出数组A与数组B有差异的部分
export const compareDifferentAndNewArray = (arrayA: any[], arrayB: any[]) => {
  let result: any[] = []
  let newArray = arrayA.filter(item => !arrayB.some(e => e.firefightingteamid === item.firefightingteamid))
  newArray.forEach(item => {
    result.push(Number(item.firefightingteamid))
  })
  console.log('result====', result)
  return result
}

/**
 * @author: silencetea
 * @name: sortNumberArray
 * @description: 数字数组排序，默认升序：从小到大
 * @param {any} fmt 要以何种格式格式化
 * @param {any} datetime 要格式化的日期时间
 * @return {*}
 */
export const datetimeFormat = (fmt: any, datetime?: any) => {
  let _this = datetime ? new Date(datetime) : new Date()
  let o: any = {
    'M+': _this.getMonth() + 1,
    'd+': _this.getDate(),
    'H+': _this.getHours(),
    'm+': _this.getMinutes(),
    's+': _this.getSeconds(),
    'S+': _this.getMilliseconds()
  }
  //因为date.getFullYear()出来的结果是number类型的,所以为了让结果变成字符串型，下面有两种方法：
  if (/(y+)/.test(fmt)) {
    //第一种：利用字符串连接符“+”给date.getFullYear()+""，加一个空字符串便可以将number类型转换成字符串。
    fmt = fmt.replace(RegExp.$1, (_this.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      //第二种：使用String()类型进行强制数据类型转换String(date.getFullYear())，这种更容易理解。
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(String(o[k]).length))
    }
  }
  return fmt
}
